from typing import Dict

from fastapi import APIRouter

from backend.app.common.response.response_schema import response_base
from backend.app.schemas.device_v2 import DeviceDetailReq, DeviceListReq, DeviceDeleteReq, \
    DeviceBatchDeleteReq, DeleteShopReq, DeviceUpdateOrCreateReq, DeviceBatchOperateReq
from backend.app.services.goods.goods import GoodsVectorService

router = APIRouter()


@router.post('/detail', summary='设备详情')
async def get_goods_detail(req: DeviceDetailReq) -> Dict:
    detail = await GoodsVectorService().get_detail_by_id(req)
    return await response_base.success(data=detail)


@router.post('/search', summary='搜索设备')
async def search_goods(req: DeviceListReq) -> Dict:
    goods_list = await GoodsVectorService().search_list(req)
    return await response_base.success(data=dict(list=goods_list, count=len(goods_list)))


@router.post('/update', summary='更新设备详情')
async def update_or_create(req: DeviceUpdateOrCreateReq):
    if not req.gid:
        return await response_base.fail(msg="请填写设备ID")
    detail = await GoodsVectorService().create_or_update(req)
    if not (detail and detail["success"]):
        return await response_base.fail(msg="修改失败", code=-1, detail=detail)
    return await response_base.success(data=detail)


@router.post('/batch_update', summary='批量更新设备详情')
async def batch_update_or_create(req: DeviceBatchOperateReq):
    if not req:
        return await response_base.fail(msg="请填写设备ID")
    for good in req.device_list:
        if not good.gid:
            return await response_base.fail(msg="请填写设备ID")

    detail = await GoodsVectorService().batch_create_or_update(req)
    if not (detail and detail["success"]):
        return await response_base.fail(msg="修改失败", code=-1, detail=detail)
    return await response_base.success(data=detail)


@router.post('/delete', summary='删除设备')
async def delete_goods(req: DeviceDeleteReq):
    if not req.gid:
        return await response_base.fail(msg="请选择您要删除的设备")

    detail = await GoodsVectorService().delete_by_id(req)
    if not (detail and detail["success"]):
        return await response_base.fail(msg="删除失败", code=-1, detail=detail)
    return await response_base.success(data=detail)


@router.post('/batch_delete', summary='批量删除设备')
async def batch_delete(req: DeviceBatchDeleteReq):
    if not req.gids:
        return await response_base.fail(msg="请选择您要删除的设备")

    detail = await GoodsVectorService().batch_delete(req)
    if not (detail and detail["success"]):
        return await response_base.fail(msg="删除失败", code=-1, detail=detail)
    return await response_base.success(data=detail)


@router.post('/delete_shop', summary='删除商户索引')
async def delete_shop(req: DeleteShopReq):
    if not req.platform:
        return await response_base.fail(msg="请选择您要删除的商户")

    detail = GoodsVectorService().delete_shop(req)
    if not (detail and detail["success"]):
        return await response_base.fail(msg="删除失败", code=-1, detail=detail)
    return await response_base.success(data=detail)
